<?php
namespace jy_area_dian;
defined('BY_JYA') or exit('error');
/*
	分销插件工具类
*/
class model extends \plugin{
	
	
	public function current_level_up_types(){
		return str_array($this->setting['level_up_types']);
	}
	public function available_areas($isid=0,$noone=false){
		$areas=file_get_contents(ROOT_D.'/core/resource/area_town.txt');
		$areas=unserialize($areas);
		if($isid){
			$newarea=array();
			if($noone){
				$newarea[]=array('title'=>'请选择省','list'=>array());
			}
			foreach($areas as $province=>$pv){
				$newpv=array();
				if($noone){
					$newpv[]=array('title'=>'请选择城市','list'=>array());
				}
				foreach($pv as $city=>$cv){
					$newcv=array();
					if($noone){
						$newcv[]=array('title'=>'请选择区','list'=>array());
					}
					foreach($cv as $district=>$dv){
						$newdv=array();
						if($noone){
							$newdv[]=array('title'=>'请选择街道','list'=>array());
						}
						foreach($dv as $street=>$sv){
							$newdv[]=array(
								'title'=>$street,
								'list'=>$sv
							);
						}
						$newcv[]=array(
							'title'=>$district,
							'list'=>$newdv
						);
					}
					$newpv[]=array(
						'title'=>$city,
						'list'=>$newcv
					);
				}
				$newarea[]=array(
					'title'=>$province,
					'list'=>$newpv
				);
			}
			$areas=$newarea;
		}
		return $areas;
	}
	public function get_area_code($area){
		$address=$area['province'];
		$oldaddress=$area['province'];
		if($area['city']){
			$address.=' '.$area['city'];
			$oldaddress.=' 直辖市';
		}
		if($area['district']){
			$address.=' '.$area['district'];
			$oldaddress.=' '.$area['district'];
		}
		if($area['street']){
			$address.=' '.$area['street'];
			$oldaddress.=' '.$area['street'];
		}
		$area_code=pdo_getcolumn('core_area',array('address'=>$address),'code');
		if(!$area_code){
			$isup=pdo_count('core_area',array(),'code');
			if(!$isup){
				require ROOT_D.'/update/update_area.php';
				$area_code=pdo_getcolumn('core_area',array('address'=>$address),'code');
			}else{
				$area_code=pdo_getcolumn('core_area',array('address'=>$oldaddress),'code');
				if(!$area_code){
					pdo_query('DROP TABLE IF EXISTS '.TABLE_PRE.'core_area;');
					require ROOT_D.'/update/update_area.php';
				}
			}
		}
		return $area_code;
	}
	/*
		订单，获取收货点
	*/
	public function get_dians($order=array()){
		if(!$order['id']){
			exi('请选择订单和收货点','error');
		}
		//用户地址
		$order['address']=json_decode($order['address'],true);
		if($order['address'] && $order['address']['name']){
			$address=$order['address'];
		}else{
			$address=pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['addressid']));
		}
		$dian_setting=$this->plugin_setting('setting');
		$data = cfc('tools')->getgeocode($address, $dian_setting['geo_key']);
		if($data['status'] == 1 && 0 < $data['count']) {
			$location = explode(',', $data['geocodes'][0]['location']);
			$lat_lng=array('lat'=>$location[1],'lng'=>$location[0]);
		}
		$prams=array('uniacid'=>$_SESSION['uniacid'],'province'=>$address['pcd']['province'],'city'=>$address['pcd']['city'],'district'=>$address['pcd']['district'],'street'=>$address['pcd']['street']);
		$dians=pdo_getall('jy_area_dian_agent',$prams);
		if(!count($dians)){
				unset($prams['street']);
				$dians=pdo_getall('jy_area_dian_agent',$prams);
				if(!count($dians)){
					unset($prams['district']);
					$dians=pdo_getall('jy_area_dian_agent',$prams);
				}
		}
		foreach($dians as $k=>&$v){
				$v['lat_lng']=json_decode($v['lat_lng'],true);
				$v['lat_lng']['lng']=$v['lat_lng']['lng']?:'114.424632';
				$v['lat_lng']['lat']=$v['lat_lng']['lat']?:'23.126942';
				$v['juli']=cfc('tools')->getdistance($lat_lng['lng'],$lat_lng['lat'],$v['lat_lng']['lng'],$v['lat_lng']['lat']);
				$v['juli']=intval($v['juli'])/1000?:2;
		}
		paixu($dians,'juli');
		return $dians;
	}
	public function order_verify($orderid=0){
		if($orderid){
			$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$orderid));
		}else{
			exi('订单ID不存在！','error','/index/');   
		}
		if($order['status']==3){
			exi('该订单已完成！不能核销！','error');
		}
		if($order['paytype']=='credit5' && $order['plugin_name']=='jy_duotuan' && $this->plugin_exists('jy_duotuan')){
			model('jy_duotuan/model')->order_receive($orderid);
		}
		$this->pdo_update('order',array('status'=>3),array('orderid'=>$order['id'],'uniacid'=>$_SESSION['uniacid']));
		model('jy_weishop/order')->order_receive($orderid);
		$dianorderid=$this->pdo_getcolumn('order',array('orderid'=>$order['id'],'uniacid'=>$_SESSION['uniacid']),'id');
		$this->model('commission')->create($order);
		$this->model('notice')->notice_upstatus($dianorderid,3);
		return true;
	}
	public function get_wxappewm($url,$pid){//pid要有唯一性
		if(!$url || !$pid || !$_SESSION['wxapp_id']){
			return false;
		}
		$rootat=ATTACHMENT_TYPE == '0'?ROOT_AT:ATTACHMENT_URL;
		$image='oss/'.$_SESSION['uniacid'].'/images/jy_area_dian/'.$pid.'.jpg';
		$isadd=cfc('files')->source_check($image);
		if(!$isadd){
			$wxapp=pdo_get('core_account_wxapp',array('id'=>$_SESSION['wxapp_id'],'uniacid'=>$_SESSION['uniacid']));
			$res=model('core/wxapp')->appcode($url,$wxapp['appid'],$wxapp['appsecret'],200);
			$path='oss/'.$_SESSION['uniacid'].'/images/jy_area_dian';
			if(!file_exists($rootat.$path) && ATTACHMENT_TYPE == '0'){
					mkdir(ROOT_AT.$path,0700, true);
			}
			if($res){
				$filename=$path.'/'.$pid.'.jpg';
				$qr=cfc('files')->source_save($res,'nochoose','png','二维码',$filename,'',$url);
			}
		}
		$ewm='oss/'.$_SESSION['uniacid'].'/images/jy_area_dian/'.$pid.'.jpg';
		return $ewm;
	}
}